package com.smbms.controller;

import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.util.PageParams;
import com.smbms.util.PageResult;
import com.smbms.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 类名：UserController
 * 作者：小涛
 * 创建时间：2021/9/8-16:12
 * 功能：
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    private Result result;

    @RequestMapping("/findPage")
    public Result findPage(PageParams pageParams) {
        PageResult<User> pageResult = userService.findPage(pageParams);
        result = new Result(true,"用户查询成功",pageResult);
        return result;
    }

    @RequestMapping("/delUser")
    public Result delUser(Integer id) {
        int count = userService.delUser(id);
        if(count > 0){
            result = new Result(true, "删除用户成功");
        }else {
            result = new Result(false, "删除用户失败");
        }
        return result;
    }

    @RequestMapping("/checkCodeAdd")
    private Result checkCodeAdd(User user) {
        int count = userService.checkCodeAdd(user);
        if (count>0){
            result = new Result(false,"该编码已被占用");
        }else {
            result = new Result(true,"该编码可以使用");
        }
        return result;
    }

    @RequestMapping("/addUser")
    private Result addUser(User user, HttpSession session) {
        //创建人，当前登录的用户
        User loginUser = (User)session.getAttribute("LOGIN_USER");
        user.setCreatedBy(loginUser.getId().intValue());

        //添加到数据库
        int count = userService.addUser(user);
        if (count>0){
            return new Result(true,"添加用户成功");
        }else {
            return new Result(false,"添加失败");
        }
    }

    @RequestMapping("/findById")
    private Result findById(Integer id) {
        User user = userService.findById(id);
        return new Result(true, "查询用户成功", user);
    }

    @RequestMapping("/checkCodeUpd")
    private Result checkCodeUpd(String userCode, String self) throws SQLException, IOException, ClassNotFoundException {
        int count = userService.countByCodeUpd(userCode, self);
        if(count > 0){
            return new Result(false, "用户编码已被占用");
        }
        return new Result(true, "用户编码可以使用");
    }

    @RequestMapping("/updUser")
    private Result updUser(User user, HttpSession session) {
        //修改人，当前登录的用户
        User loginUser = (User) session.getAttribute("LOGIN_USER");
        user.setModifyBy(loginUser.getId().intValue());

        //修改用户
        int count = userService.updUser(user);
        if(count > 0){
            return new Result(true, "修改用户成功");
        }
        return new Result(false, "修改用户失败");

    }
}
